﻿using Newtonsoft.Json;

namespace D.WebApi.Core.Dtos
{

    /// <summary>
    /// 请求反参
    /// </summary>
    public class ApiResult : ApiBaseResult
    {
        /// <summary>是否为成功</summary>
        protected bool isSuccess;

        /// <summary>是否为成功</summary>
        /// <returns>bool 值</returns>
        [JsonProperty("success")]
        public virtual bool IsSuccess
        { get => isSuccess; set { isSuccess = value; } }
         
        /// <summary>生成成功时的应答实例</summary>
        /// <returns>ApiResult 实例</returns>
        public static ApiResult Success()
        {
            return new ApiResult { Code = 200, Message = "success" };
        }

        /// <summary>生成成功时的应答实例</summary>
        /// <param name="data">应答数据</param>
        /// <param name="message">应答消息</param>
        /// <returns>ApiResult 实例</returns>
        public static ApiResult Success(Object data, string message = "success")
        {
            return new ApiResult { Code = 200, Data = data, Message = message };
        }

        /// <summary>生成失败时的应答实例</summary>
        /// <param name="message">应答消息</param>
        /// <returns>ApiResult 实例</returns>
        public static ApiResult Fail(string message)
        {
            return new ApiResult { Code = 500, Message = message };
        }

        /// <summary>参数有误</summary>
        public static readonly ApiResult ParamError = new ApiResult() { Code = 500, Message = "参数有误" };

        /// <summary>操作成功</summary>
        public static readonly ApiResult OperationSuccessful = new ApiResult() { Code = 200, Message = "操作成功" };

        /// <summary>操作失败</summary>
        public static readonly ApiResult OperationFailed = new ApiResult() { Code = 500, Message = "操作失败" };

        /// <summary>操作结果</summary>
        /// <param name="isSuccess">是否为操作成功</param>
        /// <returns>ApiResult 实例</returns>
        public static ApiResult Operation(bool isSuccess)
        {
            return isSuccess ? OperationSuccessful : OperationFailed;
        }
    }
    
    /// <summary>
    /// 返回类型
    /// </summary>
    public enum SuccessTypeEnum
    {
        Success, Error,
    }
}
